<?php
//Test si l'utilisateur est connecté
if ((empty($_SESSION["connect"])||($_SESSION["connect"] != 1))) {
	header("Location: ?pageid=1");
}

// Appel formulaire
if(isset($_POST['subform']))
{
	$info = getUserProfile($connmysql,$_SESSION["iduser"])->fetch();
	//var_dump($_POST);
	if(!empty($_POST['userbirthday'])&&!preg_match("#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#",$_POST['userbirthday']))$errors[]="La date de naissance n'est pas dans un format valide (jj/mm/aaaa).";
	if(!empty($_POST['useremail'])&&!preg_match('/^[a-zA-Z0-9&\'\.\-_\+]+\@[a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/',$_POST['useremail']))$errors[]="Veuillez entrer une adresse mail correcte.";
	if(!empty($_POST['userpassword'])&&($_POST['userpassword'] == $_POST['cuserpassword'])){$userpassword = $_POST['userpassword'];}else{$userpassword = "";}
	if(isset($_POST['showdone'])){$showdone = 1;}else{$showdone = 0;}
	if(!isset($errors))
	{/*Aucune erreur, on sauvegarde !!!!*/
		$msg = updateUser($connmysql,$_SESSION["iduser"],$_POST['userlastname'],$_POST['userfirstname'],dateToMysqlHless($_POST['userbirthday']),$_POST['useremail'], $userpassword, $showdone);
	}
	if(!empty($_POST['userpassword'])&&($_POST['userpassword'] != $_POST['cuserpassword'] ))$errors[]="Les mots de passe ne correspondent pas.";
	}


//Recup informations profil connecté


$info = getUserProfile($connmysql,$_SESSION["iduser"])->fetch();
?>
<div id="boxtitle" class="encours" >Editer profil</div>
	<?php
	// Informations
	if(isset($msg))
	{
		echo "<div id=\"msg\" class=\"info\">
				".$msg."
				</div>";
	}
	//Affichage erreurs
	if(isset($errors))
	{
		echo "<div id=\"msg\" class=\"error\">";
		foreach($errors as $e)
			echo $e."<br />";
		echo "</div>";
	}
	?>
<div id="logform">
    <form name="subform" method="POST" action="?pageid=3">
    <fieldset>
    <legend>Options accueil</legend>
	<p><img class="imgf" src="./images/play.png" />Afficher les tâches accomplies
	<input class="check" type="checkbox" name="showdone" <?php if(!empty($info["showdone"])){ echo "checked=\"checked\"";} ?> /></p>
	</fieldset>
	<fieldset>
    <legend>Informations pour <span class="green"><?php echo $_SESSION["userlogin"]; ?></span></legend>
	 <p><img class="imgf" src="./images/play.png" />
	 Nom<br>
	 <input class="textin" name="userlastname" placeholder="Nom" value="<?php echo $info["userlastname"];?>" type="text" /></p>
	 <p><img class="imgf" src="./images/play.png" />
	 Prénom<br>
	 <input class="textin" name="userfirstname" placeholder="Prénom" value="<?php echo $info["userfirstname"];?>" type="text" /></p>
	 <p><img class="imgf" src="./images/play.png" />
	 Date de naissance<br>
	 <input class="textin"  type="date" name="userbirthday" placeholder="JJ/MM/AAAA" value="<?php echo dateFromMysqlHless($info["userbirthday"]);?>" type="text" /></p>
    <p><img class="imgf" src="./images/play.png" />
	Email<br>
	<input class="textin" type="email" name="useremail" placeholder="user@mail.com" value="<?php echo $info["useremail"];?>" type="text" /></p>
	<p><img class="imgf" src="./images/play.png" />
	Changement mot de passe:<br>
      <input class="textin" name="userpassword" placeholder="Nouveau" type="password" /> </p>
	<p><input class="textin" name="cuserpassword" placeholder="Répéter" type="password" /> </p>
        </fieldset>
	<p>
      <input type="submit" class="subform" name="subform" value="Enregistrer" />
    </p>
    <p>
    <a class="links" href="?pageid=2">&lt; Retour</a>
    </p>
    </form>
</div>